<?php
/**
 * User: Jinqn
 * Date: 14-04-09
 * Time: 10:17
 */

 if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

include "Uploader.class.php";

$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
    case 'uploadimage':
        $config = array(
            "pathFormat" => $CONFIG['imagePathFormat'],
            "maxSize" => $CONFIG['imageMaxSize'],
            "allowFiles" => $CONFIG['imageAllowFiles']
        );
        $fieldName = $CONFIG['imageFieldName'];
        break;
    case 'uploadscrawl':
        $config = array(
            "pathFormat" => $CONFIG['scrawlPathFormat'],
            "maxSize" => $CONFIG['scrawlMaxSize'],
            "allowFiles" => $CONFIG['scrawlAllowFiles'],
            "oriName" => "scrawl.png"
        );
        $fieldName = $CONFIG['scrawlFieldName'];
        $base64 = "base64";
        break;
    case 'uploadvideo':
        $config = array(
            "pathFormat" => $CONFIG['videoPathFormat'],
            "maxSize" => $CONFIG['videoMaxSize'],
            "allowFiles" => $CONFIG['videoAllowFiles']
        );
        $fieldName = $CONFIG['videoFieldName'];
        break;
    case 'uploadfile':
    default:
        $config = array(
            "pathFormat" => $CONFIG['filePathFormat'],
            "maxSize" => $CONFIG['fileMaxSize'],
            "allowFiles" => $CONFIG['fileAllowFiles']
        );
        $fieldName = $CONFIG['fileFieldName'];
        break;
}

$up = new Uploader($fieldName, $config, $base64);
$file = $up->getFileInfo();

require_once libfile('class/image');
$image = new image();
if($_G['setting']['dzapp_base_thumb_width']) {
	if($image->Thumb(DISCUZ_ROOT.$file['url'], '', $_G['setting']['dzapp_base_thumb_width'], 999999, 1)) {
		$file['thumb'] = 1;
		$file['url'] = $file['url'].'.thumb.jpg';
	}
}
if(empty($_G['setting']['portalarticleimgthumbclosed']) && $_G['setting']['dzapp_base_watermark']) {
	$image->Watermark(DISCUZ_ROOT.$file['url'], '', 'portal');
	$image->Watermark(DISCUZ_ROOT.$file['url'].'.thumb.jpg', '', 'portal');
}

$setarr = array(
	'uid' => $_G['uid'],
	'username' => $_G['username'],
	'type' => 1,
	'filename' => $file['original'],
	'filetype' => $file['type'],
	'filesize' => $file['size'],
	'attachment' => $file['url'],
	'thumb' => $file['thumb'],
	'remote' => $file['remote'],
	'dateline' => $_G['timestamp'],
	'id' => $_GET['theid'],
	'idtype' => $_GET['idtype'],
);
$file['attachment_id'] = C::t('#dzapp_base#dzapp_base_attachment')->insert($setarr, true);

return json_encode($file);